package com.net.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.net.group.Specification;
import com.net.mapper.TbSpecificationMapper;
import com.net.mapper.TbSpecificationOptionMapper;
import com.net.pojo.TbSpecification;
import com.net.pojo.TbSpecificationOption;
import com.net.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;
    @Autowired
    private TbSpecificationOptionMapper optionMapper;

    @Override
    public List<TbSpecification> findAll() {
        return specificationMapper.selectAll();
    }

    @Override
    public void add(Specification specification) {
        //添加规格表数据
        specificationMapper.insert(specification.getSpec());
        //得到添加后的规格id
        Integer specId = specification.getSpec().getId();
        //添加规格选择项
        List<TbSpecificationOption> options = specification.getOptions();
        for (TbSpecificationOption option : options) {
            option.setSpecId(specId);   //关联规格选项的specId
            //添加规格选项
            optionMapper.insert(option);
        }
    }

    @Override
    public void update(Specification specification) {
        //修改主表Tb_specification
        specificationMapper.updateByPrimaryKey(specification.getSpec());
        //修改规格选项的思路：先根据specId删除规格选项，再重新添加
        //根据外键specid删除规格选项
        Example example = new Example(TbSpecificationOption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specId", specification.getSpec().getId());
        optionMapper.deleteByExample(example);
        //重新添加规格选项的值
        for (TbSpecificationOption option : specification.getOptions()) {
            option.setSpecId(specification.getSpec().getId());
            optionMapper.insert(option);
        }
    }

    @Override
    public Specification findOne(Long id) {
        //查询规格对象
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        //查询此规格下的规格选项列表
        //定义查询实例
        Example example = new Example(TbSpecificationOption.class);
        //添加查询条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("specId", id);
        //开始进行外键查询
        List<TbSpecificationOption> options = optionMapper.selectByExample(example);
        //创建组合对象
        Specification specification = new Specification();
        specification.setSpec(tbSpecification);
        specification.setOptions(options);
        return specification;
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            //删除规格
            specificationMapper.deleteByPrimaryKey(id);
            //删除规格选项
            Example example = new Example(TbSpecificationOption.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("specId",id);
            optionMapper.deleteByExample(example);
        }
    }

    @Override
    public List<Map> findSpecList() {
        return null;
    }
}
